package com.example.medicalequipment.ui.activity;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.example.medicalequipment.R;
import com.example.medicalequipment.controller.UserController;
import com.example.medicalequipment.entity.User;

public class LoginActivity extends AppCompatActivity {
    private EditText etUsername, etPassword;
    private Button btnLogin;
    private TextView tvRegister;
    private UserController userController;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);

        etUsername = findViewById(R.id.etUsername);
        etPassword = findViewById(R.id.etPassword);
        btnLogin = findViewById(R.id.btnLogin);
        tvRegister = findViewById(R.id.tvRegister);

        userController = new UserController(this);

        btnLogin.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String username = etUsername.getText().toString();
                String password = etPassword.getText().toString();

                if (username.isEmpty() || password.isEmpty()) {
                    Toast.makeText(LoginActivity.this, "用户名和密码不能为空", Toast.LENGTH_SHORT).show();
                    return;
                }

                userController.login(username, password, new UserController.LoginCallback() {
                    @Override
                    public void onLoginResult(User user) {
                        if (user != null) {
                            Toast.makeText(LoginActivity.this, "登录成功", Toast.LENGTH_SHORT).show();

                            // 根据用户类型跳转不同界面
                            if (user.getUserType() == User.TYPE_REPAIR) {
                                // 维修人员跳转到维修审批页面
                                Intent intent = new Intent(LoginActivity.this, RepairApprovalActivity.class);
                                startActivity(intent);
                            } else {
                                // 普通用户跳转到主页
                                Intent intent = new Intent(LoginActivity.this, HomeActivity.class);
                                intent.putExtra("userId", user.getId());
                                intent.putExtra("userType", user.getUserType());
                                startActivity(intent);
                            }
                            finish();
                        } else {
                            Toast.makeText(LoginActivity.this, "用户名或密码错误", Toast.LENGTH_SHORT).show();
                        }
                    }
                });
            }
        });

        tvRegister.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startActivity(new Intent(LoginActivity.this, RegisterActivity.class));
            }
        });
    }
}